package com.qdlc.p2p.dal.model;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import com.qdlc.p2p.common.constant.Constant;
import com.qdlc.p2p.common.exception.FinanFlowExecption;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.FreemarkerUtil;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.dal.common.LogTemplateHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.dao.AccountDao;
import com.qdlc.p2p.dal.dao.AccountLogDao;
import com.qdlc.p2p.dal.dao.AdditionalFlowProjectDao;
import com.qdlc.p2p.dal.dao.AdditionalFlowRepayPlanDao;
import com.qdlc.p2p.dal.dao.AdditionalFlowYieldPlanDao;
import com.qdlc.p2p.dal.dto.Account;
import com.qdlc.p2p.dal.dto.AccountLog;
import com.qdlc.p2p.dal.dto.AdditionalFlowProject;
import com.qdlc.p2p.dal.dto.AdditionalFlowRepayPlan;
import com.qdlc.p2p.dal.dto.AdditionalFlowYieldPlan;
import com.qdlc.p2p.dal.dto.User;

/**
 * 待还计划model
 * @author wjx
 * @version 2.0
 * @date 2015年5月16日 下午3:22:45
 */
public class RepayPlanModel extends AdditionalFlowRepayPlan {


	private static Logger logger = Logger.getLogger(RepayPlanModel.class);
	
	
	/**
	 * 初始化，未还
	 */
	public static final byte INIT_STATUS = 0;
	
	
	/**
	 * 已还
	 */
	public static final byte YES_REPAY_STATUS = 1;
	
	/**
	 * 还款处理初始状态
	 */
	public static final byte INIT_WEB_STATUS =0 ;
	
	
	/**
	 * 还款处理中
	 */
	public static final byte YES_WEB_STATUS = 1;
	
	/**
	 * 项目名称
	 */
	private String projectName;
	
	/**
	 * uuid
	 */
	private String uuid;
	
	/**
	 * 当期还款总额
	 */
	private double repaySumAccount;
	
	/**
	 * 时间
	 */
	private String dateStr;
	
	
	/**
	 * 创建model
	 * @param repayPlan
	 * @return
	 */
	public static  RepayPlanModel instance(AdditionalFlowRepayPlan repayPlan){
		RepayPlanModel repayPlanModel = new RepayPlanModel();
		BeanUtils.copyProperties(repayPlan, repayPlanModel);
		return repayPlanModel;
	}
	
	
	
	public  AdditionalFlowRepayPlan prototype(){
		AdditionalFlowRepayPlan plan = new AdditionalFlowRepayPlan();
		BeanUtils.copyProperties(this, plan);
		return plan;
	}
	
	/**
	 * 校验还款人信息
	 */
	public void checkRepayUser(){
		//校验资金信息
		AccountDao accountDao = (AccountDao) BeanUtil.getBean("accountDao");
		Account account = accountDao.findByUserId(getUser().getUserId());
		double copie = getRepayCopies();  //收益
		double money = getRepayMoney(); //本金
		double sunAccount = BigDecimalUtil.add(copie,money);
		if (account.getUseMoney()< sunAccount) {
			throw new FinanFlowExecption(MessageUtil.getMessage("MF103003002"));
		}
	}
	
	/**
	 * 校验当前项目是否还有全部还款完成
	 */
	public void checkAllRepayProject(){
		AdditionalFlowRepayPlanDao flowRepayPlanDao = (AdditionalFlowRepayPlanDao) BeanUtil.getBean("additionalFlowRepayPlanDao");
		AdditionalFlowProjectDao additionalFlowProjectDao = (AdditionalFlowProjectDao)BeanUtil.getBean("additionalFlowProjectDao");
		int count = flowRepayPlanDao.getAllWaitRepay(getFlowProject().getId());
		//获取项目对象
		AdditionalFlowProject proj = additionalFlowProjectDao.getAdditionalFlowProjectById(getFlowProject().getId());
		if (count ==0 && proj.getTotalCopies() ==proj.getYesCopies()) { //没有待还记录，将借款状态修改掉
			int result = additionalFlowProjectDao.updateFlowProjectStatus(FlowProjectModel.PROJECT_END_STATUS, getFlowProject().getId());
			logger.info("修改理财项目状态：result："+result+",id:"+getFlowProject().getId());
		}
	}

	public String getProjectName() {
		return projectName;
	}



	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}



	public String getUuid() {
		return uuid;
	}



	public void setUuid(String uuid) {
		this.uuid = uuid;
	}



	public double getRepaySumAccount() {
		return repaySumAccount;
	}



	public void setRepaySumAccount(double repaySumAccount) {
		this.repaySumAccount = repaySumAccount;
	}



	public String getDateStr() {
		return dateStr;
	}



	public void setDateStr(String dateStr) {
		this.dateStr = dateStr;
	}

}
